//
// Created by Zeppyrz on 2025/11/6.
//

#include "DirTask.h"

#include <iostream>

#include "ZeDiskClient.h"

namespace ZeDiskGUI {

bool DirTask::Stop() {
  // nothing to do
  return CommonTask::Stop();
}

void DirTask::HandleMessage(const Message* msg) {
  CommonTask::HandleMessage(msg);
  // 接受服务发出的目录
  switch (msg->head.type) {
    case MSG_TYPE_DIRLIST:
      std::cout << "MSG_TYPE_DIRLIST" << std::endl;
      DirectoryListCallback(msg->data);
      break;
    default:
      break;
  }
}

void DirTask::ConnectCallback() {
  CommonTask::ConnectCallback();
  Message msg;
  msg.head.type = MSG_TYPE_GETDIR;
  auto &root = ZeDiskClient::GetInstance().root();
  msg.head.size = root.size();
  msg.data = const_cast<char*>(root.c_str());
  // root 为空 不发送
  WriteMessage(&msg);
}

}  // namespace ZeDiskGUI